
#include "YoungThread.h"


void YoungThread::Pause()
{
	SuspendThread(m_hThread);
}
void YoungThread::Continue()
{
	ResumeThread(m_hThread);
}
void YoungThread::Terminated()
{
	Continue();
	EnterCriticalSection(&m_ce);
	m_bTerminated = TRUE;
	LeaveCriticalSection(&m_ce);
}

void YoungThread::WaitSync()
{
	WaitForSingleObject(m_hThread, INFINITE);
}
void YoungThread::WaitTerminated()
{
	Terminated();
	WaitForSingleObject(m_hThread, INFINITE);
}
BOOL YoungThread::GetTerminated()
{
	BOOL bRet = FALSE;
	EnterCriticalSection(&m_ce);
	bRet = m_bTerminated ;
	LeaveCriticalSection(&m_ce);
	return bRet;
}
YoungThread::YoungThread():m_hThread(NULL),m_bTerminated(FALSE)
{
	InitializeCriticalSection (&m_ce);
}
YoungThread::~YoungThread()
{
	DeleteCriticalSection(&m_ce);
}

void YoungThread::Start(LPTHREAD_START_ROUTINE lpFun)
{
	if (m_hThread != NULL)
	{
		Terminated();
	}
	if (lpFun == NULL)
	{
		ClassThreadProc pProc = (ClassThreadProc)&YoungThread::ThreadFuc;
		m_bTerminated = FALSE;
		m_hThread = CreateThread(NULL, 0, *(PTHREAD_START_ROUTINE*)&pProc, (LPVOID)this,0,0);
		if (m_hThread == NULL || m_hThread == INVALID_HANDLE_VALUE)
		{
			Terminated();
			m_hThread = NULL;
		}
	}
	else
	{
		m_bTerminated = FALSE;
		m_hThread = CreateThread(NULL, 0, *(PTHREAD_START_ROUTINE*)&lpFun, (LPVOID)this,0,0);
		if (m_hThread == NULL || m_hThread == INVALID_HANDLE_VALUE)
		{
			Terminated();
			m_hThread = NULL;
		}
	}
	

}
void YoungThread::run()
{

}
DWORD YoungThread::ThreadFuc(LPVOID lp) 
{
	YoungThread *YThis = (YoungThread*)lp;
	YThis->run();
	return 0 ;
};
